package com.feisuanyz.service.impl;

import com.feisuanyz.dao.UserMapper;
import com.feisuanyz.pojo.domain.UserDO;
import com.feisuanyz.pojo.dto.UserDTO;
import com.feisuanyz.pojo.dto.UserLoginDTO;
import com.feisuanyz.pojo.query.UserQuery;
import com.feisuanyz.pojo.vo.UserVO;
import com.feisuanyz.service.UserService;
import java.lang.Boolean;
import java.lang.Integer;
import java.lang.Override;
import java.lang.String;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.feisuanyz.exception.BusinessException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.Map;
import com.feisuanyz.constant.ResultCodeConstant;
import java.util.Date;

/**
 * 用户管理的实现
 *
 * @author Chenke Zhao
 * @date 2025-03-06 10:18:05
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean registerUser(UserDTO userDTO) {
        // 检查用户名和邮箱是否已存在
        QueryWrapper<UserDO> wrapper = Wrappers.query();
        wrapper.eq("username", userDTO.getUsername());
        wrapper.or().eq("email", userDTO.getEmail());
        if (userMapper.selectCount(wrapper) > 0) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 生成密码哈希
        String passwordHash = generatePasswordHash(userDTO.getPassword());
        // 插入用户信息到users表
        UserDO userDO = new UserDO();
        userDO.setUsername(userDTO.getUsername());
        userDO.setPasswordHash(passwordHash);
        userDO.setEmail(userDTO.getEmail());
        userDO.setCreateBy(userDTO.getUsername());
        userDO.setCreateTime(new Date());
        userMapper.insert(userDO);
        return true;
    }

    @Override
    public String loginUser(UserLoginDTO userLoginDTO) {
        // 检查用户名是否存在
        QueryWrapper<UserDO> wrapper = Wrappers.query();
        wrapper.eq("username", userLoginDTO.getUsername());
        UserDO userDO = userMapper.selectOne(wrapper);
        if (userDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 校验密码哈希是否匹配
        if (!validatePasswordHash(userLoginDTO.getPassword(), userDO.getPasswordHash())) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 生成JWT token
        String token = generateJwtToken(userDO);
        return token;
    }

    @Override
    public UserVO validateUser(UserQuery userQuery) {
        // 验证JWT token的有效性
        if (!validateJwtToken(userQuery.getToken())) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 查询用户信息
        UserDO userDO = userMapper.selectById(getUserIdFromToken(userQuery.getToken()));
        if (userDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 转换为UserVO
        UserVO userVO = new UserVO();
        userVO.setId(userDO.getId());
        userVO.setUsername(userDO.getUsername());
        userVO.setEmail(userDO.getEmail());
        return userVO;
    }

    private String generatePasswordHash(String password) {
        // 这里使用简单的哈希生成方法，实际应用中请使用更安全的方法
        return org.apache.commons.lang3.StringUtils.reverse(password);
    }

    private Boolean validatePasswordHash(String password, String passwordHash) {
        // 这里使用简单的哈希校验方法，实际应用中请使用更安全的方法
        return generatePasswordHash(password).equals(passwordHash);
    }

    private String generateJwtToken(UserDO userDO) {
        // 这里使用简单的token生成方法，实际应用中请使用更安全的方法
        return "token_" + userDO.getId();
    }

    private Boolean validateJwtToken(String token) {
        // 这里使用简单的token校验方法，实际应用中请使用更安全的方法
        return token != null && token.startsWith("token_");
    }

    private Integer getUserIdFromToken(String token) {
        // 这里使用简单的token解析方法，实际应用中请使用更安全的方法
        return Integer.parseInt(token.substring(6));
    }
}
